package com.hetang.maijiayun.adaptertools.utils;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;

public class JacksonUtil {

    private static final ObjectMapper om;
    private static final String defaultDateFormat = "yyyy-MM-dd HH:mm:ss";

    static {
        om = new ObjectMapper();
        om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
                false);
    }

    /**
     * 根据json字符串获得指定类型的Java对象
     * 
     * @param json
     * @param clazz
     * @param dateFormat
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> T getBean(String json, Class<T> clazz,
            DateFormat dateFormat) throws JsonParseException,
            JsonMappingException, IOException {
        om.setDateFormat(dateFormat);
        return (T) om.readValue(json, clazz);
    }

    /**
     * 根据json字符串获得指定类型的Java对象
     * 
     * @param json
     * @param clazz
     * @param dateFormat
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> T getBean(String json, Class<T> clazz, String dateFormat)
            throws JsonParseException, JsonMappingException, IOException {
        DateFormat format = new SimpleDateFormat(dateFormat);
        return getBean(json, clazz, format);
    }

    /**
     * 根据json字符串获得指定类型的Java对象
     * 
     * @param json
     * @param clazz
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> T getBean(String json, Class<T> clazz)
            throws JsonParseException, JsonMappingException, IOException {
        return getBean(json, clazz, defaultDateFormat);
    }

    /**
     * 根据json字符串数组获得元素为指定Java类型的List
     * 
     * @param jsan
     * @param clazz
     * @param dateFormat
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> List<T> getList(String jsan, Class<T> clazz,
            DateFormat dateFormat) throws JsonParseException,
            JsonMappingException, IOException {
        om.setDateFormat(dateFormat);
        om.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        return om.readValue(jsan, TypeFactory.defaultInstance()
                .constructCollectionType(List.class, clazz));
    }

    /**
     * 根据json字符串数组获得元素为指定Java类型的List
     * 
     * @param jsan
     * @param clazz
     * @param dateFormat
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> List<T> getList(String jsan, Class<T> clazz,
            String dateFormat) throws JsonParseException, JsonMappingException,
            IOException {
        DateFormat format = new SimpleDateFormat(dateFormat);
        return getList(jsan, clazz, format);
    }

    /**
     * 根据json字符串数组获得元素为指定Java类型的List
     * 
     * @param jsan
     * @param clazz
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> List<T> getList(String jsan, Class<T> clazz)
            throws JsonParseException, JsonMappingException, IOException {
        return getList(jsan, clazz, defaultDateFormat);
    }

    /**
     * 根据json字符串数组获得元素为指定Java类型的数组
     * 
     * @param jsan
     * @param clazz
     *            这个参数要这样传T[].class
     * @param dateFormat
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> T getArray(String jsan, Class<T> clazz,
            DateFormat dateFormat) throws JsonParseException,
            JsonMappingException, IOException {
        om.setDateFormat(dateFormat);
        return om.readValue(jsan, clazz);
    }

    /**
     * 根据json字符串数组获得元素为指定Java类型的数组
     * 
     * @param jsan
     * @param clazz
     *            这个参数要这样传T[].class
     * @param dateFormat
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> T getArray(String jsan, Class<T> clazz, String dateFormat)
            throws JsonParseException, JsonMappingException, IOException {
        DateFormat format = new SimpleDateFormat(dateFormat);
        return getArray(jsan, clazz, format);
    }

    /**
     * 根据json字符串数组获得元素为指定Java类型的数组
     * 
     * @param jsan
     * @param clazz
     *            这个参数要这样传T[].class
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> T getArray(String jsan, Class<T> clazz)
            throws JsonParseException, JsonMappingException, IOException {
        return getArray(jsan, clazz, defaultDateFormat);
    }

    public static String getJson(Object obj, DateFormat dateFormat)
            throws JsonGenerationException, JsonMappingException, IOException {
        om.setDateFormat(dateFormat);
        return om.writeValueAsString(obj);
    }

    public static String getJson(Object obj, String dateFormat)
            throws JsonGenerationException, JsonMappingException, IOException {
        DateFormat format = new SimpleDateFormat(dateFormat);
        return getJson(obj, format);
    }

    public static String getJson(Object obj) throws JsonGenerationException,
            JsonMappingException, IOException {
        return getJson(obj, defaultDateFormat);
    }
}
